package pinyinJava.executors;

import pinyinJava.bean.DBean;
import pinyinJava.persistence.IDbPersistence;
import pinyinJava.persistence.SqlitePer;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class ZyAddIdExecutor {

    public void execute() throws SQLException {
        File dir = new File("pinyin-db");
        IDbPersistence dbPersistence = new SqlitePer();
        Sim2TraExecutor sim2TraExecutor = new Sim2TraExecutor();
        for (File file : dir.listFiles()) {
            if (!file.getName().endsWith(".db0")) continue;
            HashMap<String, ArrayList<DBean>> map = new HashMap<>();
            Connection connection = dbPersistence.open(file.getAbsolutePath());
            ArrayList<String> list = dbPersistence.tables(connection);
            for (String s : list) {
                ArrayList<DBean> dList = dbPersistence.read(connection, s);
                for (DBean d : dList) d.setWord(sim2TraExecutor.execute(d.getWord()));
                map.put(s, dList);
            }
            dbPersistence.close(connection);
            connection = dbPersistence.open(file.getAbsolutePath() + ".new");
            for (String tName : map.keySet()) {
                System.out.println(tName);
                ArrayList<DBean> dList = map.get(tName);
                dbPersistence.save(connection, tName, dList);
            }
            dbPersistence.close(connection);
        }
        System.out.println("done");
    }
}
